﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using System.Windows.Forms;
using Ecreate.Popedom;
namespace testHibernate
{
    public class HibernateInstance 
    {
        public ISessionFactory getSessionFactory()
        {
            IDictionary<string, string> dictionary = new System.Collections.Generic.Dictionary<string, string>();
            NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            string hbmpath = @"C:\testhibernate\mssql.hbm.xml";
            string popedomAssembly = "Ecreate.Popedom";
            config.Configure(hbmpath);
            config.AddAssembly(popedomAssembly);
            return config.BuildSessionFactory();
        }
        public HibernateInstance()
        {



        }
        public void search()
        {
            ISessionFactory factory = this.getSessionFactory();
            ISession session = factory.OpenSession();


            //tGroup model = new tGroup();
            tGroup model = session.Get<tGroup>(long.Parse("3"));
            tGrouplevel level = session.Get<tGrouplevel>("1");
            model.Id = long.Parse("3");
            model.Groupname = "宣传员";
            model.Grouplevel = level;
            model.Adminflag = 0;
            session.Transaction.Begin();
            session.Update(model);
            session.Transaction.Commit();
            //model.Id = long.Parse(this.hid_id.Value);
            //model.Groupname = this.txt_name.Text;
            //model.Adminflag = 0;
            //model.Grouplevel = grouplevel;
            //model.UpdateGroup(group);


            //tGroup model = session.Get<tGroup>(long.Parse("3"));
            //tGrouplevel level = session.Get<tGrouplevel>("3");
            //model.Groupname = "宣传员";
            //model.Grouplevel = level;
            //model.Adminflag = 0;
            ////session.Transaction.Begin();
            //session.Update(model);
            //session.Flush();
            //session.Transaction.Commit();
            //session.Transaction.Commit();


            //ISessionFactory factory = this.getSessionFactory();
            //ISession session = factory.OpenSession();
            //string hsql = " from tGroup t  where t.Grouplevel.Flevel=0 and   1=1 ";
            //IList<tGroup> list = session.CreateQuery(hsql).List<tGroup>();

        }

        public void insertMenu()
        {
            ISessionFactory factory = this.getSessionFactory();
            ISession session = factory.OpenSession();
            Ecreate.Popedom.tMenu menu = new Ecreate.Popedom.tMenu();
            menu.Menucaption = "abcdefg";
            menu.Menuurl = "snake.url";
            session.Save(menu);

        }






    }

}
